/**
 * 
 */
package kr.or.javacafe.common.util;

import org.springframework.beans.support.PagedListHolder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;

/**
 * @author mousing
 *
 */
public class PagingUtil {
	
	protected final static Log logger = LogFactory.getLog(PagingUtil.class);
	
	public static String getPagedList(String URI, PagedListHolder pagedList, int page, Object command){
		
		StringBuffer sb = new StringBuffer();
		
		int window = 10;
		int currentBlock = (int) Math.ceil((double) page / window);
		int stPage = (currentBlock - 1) * window + 1;
		int edPage = stPage + window - 1;
		int totalPage = pagedList.getPageCount();
		
		if (edPage > totalPage) {
			edPage = totalPage;
		}
		
		String link = URI+"?";
		HashMap map = (HashMap)command;
		Set hSet = map.keySet();
		Iterator iter = hSet.iterator();
		
		String key = "";
		while( iter.hasNext() ){
			key = (String)iter.next();
			link += "&"+key+"="+map.get(key);
		}
		
		//logger.debug("%%%%%%%%%%%%% link : "+link);
		//logger.debug(" page: "+page);
		
		sb.append("<table cellpadding='0' cellspacing='0' border='0' width='100%'>");
		sb.append("<tr align='center'>");
		sb.append("<td>&nbsp;</td>");
		sb.append("<td width='20' align='center' style='padding-top:2px;'>");
		
		//prev page link
		sb.append(PagingUtil.getPrevBlock(URI, page, command));
		sb.append("</td>");
		sb.append("<td width='5'></td>");
		
		sb.append("<td width='130' class='txt_b'>");
		for( int i=stPage; i <= edPage; i++ ){
			if( i == page ){
				sb.append("<strong>");
			}else{
				sb.append("<a href='"+link+"&page="+i+"'>");
			}
			sb.append(i);
			sb.append("</a>");
			if( i == page ){
				sb.append("</strong>");
			}
			if( i != edPage ){
				sb.append(".&nbsp;");
			}
		}
		sb.append("</td>");
		
		sb.append("<td width='5'></td>");
		sb.append("<td width='20' align='center' style='padding-top:2px;'>");
		//next page link
		sb.append(PagingUtil.getNextBlock(URI, page, totalPage, command));
		sb.append("</td>");
		sb.append("<td>&nbsp;</td>");
		
		sb.append("</tr>");
		sb.append("</table>");
		
		//logger.debug(sb.toString());
		
		return sb.toString();
		
	}
	
	public static String getPrevBlock(String URI, int page, Object command){
		
		StringBuffer sb = new StringBuffer();
		
		int window = 10;
		int currentBlock = (int) Math.ceil((double) page / window);
		int prevPage = (currentBlock - 1) * window;
		
		if( prevPage != 0 ){
			
			String link = URI+"?";
			HashMap map = (HashMap)command;
			Set hSet = map.keySet();
			Iterator iter = hSet.iterator();
			
			String key = "";
			while( iter.hasNext() ){
				key = (String)iter.next();
				link += "&"+key+"="+map.get(key);
			}
			
			sb.append("<a href='"+link+"&page="+prevPage+"'>");
		}
		sb.append("<<");
		sb.append("</a>");
		
		return sb.toString();
	}
	
	public static String getNextBlock(String URI, int page, int totalPage, Object command){
		
		StringBuffer sb = new StringBuffer();
		
		int window = 10;
		int currentBlock = (int) Math.ceil((double) page / window);
		int stPage = (currentBlock - 1) * window + 1;
		int nextPage = stPage + window;
		//logger.debug("%%%%%%%%%%%%%% currentBlock: "+currentBlock);
		
		if( nextPage <= totalPage ){
			String link = URI+"?";
			HashMap map = (HashMap)command;
			Set hSet = map.keySet();
			Iterator iter = hSet.iterator();
			
			String key = "";
			while( iter.hasNext() ){
				key = (String)iter.next();
				link += "&"+key+"="+map.get(key);
			}
			
			sb.append("<a href='"+link+"&page="+nextPage+"'>");
		}
		
		sb.append(">>");
		sb.append("</a>");
		
		return sb.toString();
	}
}
